Skip to content

[BugFix] Fix GROUP BY expression resolution in SELECT/HAVING/ORDER BY#5548

Merged
mengweieric merged 1 commit into
opensearch-project:mainfrom
dai-chen:fix/sql-groupby-expression-resolution
Jun 16, 2026
Merged

[BugFix] Fix GROUP BY expression resolution in SELECT/HAVING/ORDER BY#5548
mengweieric merged 1 commit into
opensearch-project:mainfrom
dai-chen:fix/sql-groupby-expression-resolution

Conversation

@dai-chen

@dai-chen dai-chen commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Description

A SELECT, HAVING or ORDER BY expression matching a GROUP BY expression (e.g. SELECT LENGTH(name) FROM t GROUP BY LENGTH(name)) failed with "Field [name] not found", because it was re-evaluated against base fields that no longer exist after aggregation. This change makes such an expression reuse the already-computed group-key column. There is no impact on PPL, since PPL grouping clauses only allow field references and span (never scalar function expressions).

  • Known limitation: the group-by function expression must be matched by exact function name, so a case-mismatched reference such as SELECT length(name) … GROUP BY LENGTH(name) is not resolved and would require larger changes to canonicalize identifier names during AST building/analysis.

Related Issues

Part of #5248

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@dai-chen dai-chen self-assigned this Jun 12, 2026
@dai-chen dai-chen added PPL Piped processing language bugFix SQL and removed PPL Piped processing language labels Jun 12, 2026
A SELECT/HAVING/ORDER BY expression matching a GROUP BY expression now
resolves to the materialized group-key column via a context-scoped index
checked in visitFunction, instead of recomputing it from base fields the
aggregation removed (which failed with "Field not found").

Signed-off-by: Chen Dai <daichen@amazon.com>
@mengweieric mengweieric merged commit 5de4533 into opensearch-project:main Jun 16, 2026
42 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants